package com.wy.time;

//目前先简单点考虑 时间， 不考虑闰年，等等。 而且需要增加一个输出是中文还是英文，中文的年对应英文的 year 这个意思。 慢慢做。 或者是不考虑 闰年，最大单位是天
public class Cost {

    public static String getCostTimeByMs(long timestamp) {
        final long SECOND_MILLIS = 1000;
        final long MINUTE_MILLIS = 60 * SECOND_MILLIS;
        final long HOUR_MILLIS = 60 * MINUTE_MILLIS;
        final long DAY_MILLIS = 24 * HOUR_MILLIS;
        final long YEAR_MILLIS = 365 * DAY_MILLIS;

        long years = timestamp / YEAR_MILLIS;
        timestamp %= YEAR_MILLIS;
        long days = timestamp / DAY_MILLIS;
        timestamp %= DAY_MILLIS;
        long hours = timestamp / HOUR_MILLIS;
        timestamp %= HOUR_MILLIS;
        long minutes = timestamp / MINUTE_MILLIS;
        timestamp %= MINUTE_MILLIS;
        long seconds = timestamp / SECOND_MILLIS;

        // 构建格式化字符串
        StringBuilder timeStringBuilder = new StringBuilder();
        if (years > 0) timeStringBuilder.append(years).append(" year ");
        if (days > 0) timeStringBuilder.append(days).append(" day ");
        if (hours > 0) timeStringBuilder.append(hours).append(" hour ");
        if (minutes > 0) timeStringBuilder.append(minutes).append(" minutes ");
        if (seconds > 0) timeStringBuilder.append(seconds).append(" seconds ");
        if (timeStringBuilder.length() > 0) {
            timeStringBuilder.deleteCharAt(timeStringBuilder.length() - 1);
        }
        return timeStringBuilder.toString();
    }
}
